package com.example.forumsystem.services.impl;

import com.example.forumsystem.common.AppResult;
import com.example.forumsystem.common.ResultCode;
import com.example.forumsystem.dao.MessageMapper;
import com.example.forumsystem.exception.ApplicationException;
import com.example.forumsystem.model.Message;
import com.example.forumsystem.model.User;
import com.example.forumsystem.services.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageServiceImpi implements IMessageService {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private UserServiceImpi userServiceImpi;
    @Override
    public void create(Message message) {
        // 参数校验
        if (message == null || message.getPostUserId() == null
                || message.getReceiveUserId() == null || message.getContent() == null) {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验接收者是否存在
        User user = userServiceImpi.selectById(message.getReceiveUserId());
        if(user == null || user.getDeleteState() == 1 || user.getState() == 1) {
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        // 设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        // 调用DAO
        int row = messageMapper.insertSelective(message);
        // 校验是否成功
        if(row != 1) {
            log.info(ResultCode.ERROR_SERVICES.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {

        // 校验用户是否存在
        User user = userServiceImpi.selectById(receiveUserId);

        if(user == null || user.getId() <= 0
                || user.getState() == 1 || user.getDeleteState() == 1) {
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }

        // 调用DAo
        Integer count = messageMapper.selectUnreadCount(receiveUserId);

        return count;
    }

    @Override
    public Message selectById(Long id) {
        if(id == null) {
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAo
        Message result = messageMapper.selectByPrimaryKey(id);
        // 返回
        return result;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        // 校验id的合法性
        if(receiveUserId == null) {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Message> result = messageMapper.selectByReceiveUserId(receiveUserId);
        return result;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        // 校验接受到的值是否合法
        if(id == null || state == null || state < 0 || state > 2) {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 获取站内信
        Message existsMessage = selectById(id);
        if(existsMessage == null || existsMessage.getDeleteState() == 1) {
            log.info(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        // 构建修改对象
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date = new Date();
        updateMessage.setUpdateTime(date);
        // 调用DAO
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);

        // 校验是否修改成功
        if (row != 1) {
            log.info(ResultCode.ERROR_SERVICES.toString() + "站内信状态修改失败, messageId: " + id );
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void reply(Long repliedId, Message message) {
        // 根据站内信id,校验站内信对象
        Message existsMessage = selectById(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            log.info(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }

//        // 初始化对象
//        Message updateMessage = new Message();
//        updateMessage.setPostUserId(message.getReceiveUserId()); // 发送者
//        updateMessage.setReceiveUserId(message.getPostUserId()); // 接收者
//        updateMessage.setContent(message.getContent()); // 正文
//
//        Date date = new Date();
//        updateMessage.setUpdateTime(date);
//        updateMessage.setCreateTime(date);

        // 创建站内信
        create(message);
        // 修改站内信状态
        updateStateById(repliedId, (byte) 2);
        log.info("回复成功, repliedId: " + repliedId);
    }
}
